<template>
	<view class="page">
		<view class="container">
		        <view class="imageRow" >
					<u-image :showLoading="true" style="margin-left:50rpx;" :src="imgBaseUrl3+detail.pic" width="460rpx" height="460rpx"></u-image>
		        </view>
		        <view class="rowTitle">{{detail.commodityName}}</view>
		        <view class="row">
		          <view class="label">{{'价格'}}：</view>
		          <text class="text money">￥{{buy.price ? buy.price : 0}}</text>
		        </view>
		        <view class="row" v-for="(item,index) in detail.specificationList" :key="index">
		          <view class="label">{{item.name}}：</view>
		          <view  class="val">
		          	<u-radio-group
		          	   placement="row" 
		          	 >
		          	   <u-radio v-for="(item2,index2) in item.itemList" :key="index2"
		          	     :customStyle="{marginLeft: '8px'}"
		          	     :label="item2.name" @change="radionChange(item,item2,index)"
		          	     :name="item2.name"
		          	    >
		          	   </u-radio>
		          	 </u-radio-group>
		          </view>
		        </view>
				<view class="row">
					 <view class="label">数量：</view>
					<u-number-box v-model="buy.num" @change="valChange"></u-number-box>
				</view>
				
				<view class="row" v-if="isOrder==1">
				  <view class="label">{{'实付'}}：</view>
				  <text class="text money">{{'￥1000'}}</text>
				</view>
				<view class="row" v-if="isOrder==1">
				  <view class="label">{{'状态'}}：</view>
				  <text class="text">{{'已支付'}}</text>
				</view>
		</view>
		<view class="footContent" v-if="isOrder==2">
			<view class="handleBtn" style="width: 380rpx;" @click="goCart">
				加入购物车
			</view>
			<view class="handleBtn" @click="goPay">
				购买
			</view>
		</view>
		<view class="footContent" v-if="isOrder==1">
			<view class="handleBtn" style="width: 380rpx;" @click="goBack">
				返回
			</view>
			<view class="handleBtn cancelBtn" @click="handleCancel">
				取消订单
			</view>
		</view>
	</view>
</template>

<script>
	import {getCommodityDetailApi,} from '@/common/api/commonApi/uniformshop.js'
	export default {
		data() {
			return {
				detail:{},
				currentPrice: 0,
				id: '',
				size: '',
				sizeList:[
					{
						label: 'xl',
						value: 'xl'
					},
					{
						label: 'xxl',
						value: 'xxl'
					},
					{
						label: 'xxxl',
						value: 'xxxl'
					},
				],
				specificatArr:[],
				isOrder: 2,
				buy:{id:"",price:null,commodityName:null,unitprice:null,num:1,specifications:[],specificationNames:""},
			}
		},
		components:{
		},
		methods: {
			valChange(e){
				this.buy.num = e.value;
				this.buy.price = this.buy.num*this.buy.unitprice;
				console.log(e)
			},
			suitablesp(item1,item2){
	
				console.log(item1)
				let tempitem1 = JSON.parse(item1);
				console.log(item2)
				
				if(tempitem1.length!=item2.length){
					return false;
				}
				
				
				let resf = true;
				for(let j = 0;j<item2.length;j++){
					let res = false;
				for(let i = 0;i<tempitem1.length;i++){
					if(tempitem1[i].code == item2[j].code && tempitem1[i].value==item2[j].value){
						res = true;
					}
				}
				resf = resf && res
				}
				return resf;
			},
			radionChange(item,item2,index){
				console.log(item);
				console.log(item2);
				
				let specification = {};
				specification.code = item.id;
				specification.value=item2.id;
				specification.name = item.name;
				specification.itemName = item2.name;
				
				let j = -1;
				for(let i = 0;i<this.buy.specifications.length;i++){
					if(this.buy.specifications[i].code==item.id){
						j = i;
					}
				}
				if(j!=-1){
				this.buy.specifications.splice(j,1);
				}
				this.buy.specifications.push(specification);
				
				console.log(this.buy.specifications);
				

				
				for(let k = 0;k<this.detail.commoditySpecifications.length;k++){
					let kitem = this.detail.commoditySpecifications[k].specifications;
					let resk = this.suitablesp(kitem,this.buy.specifications);
					if(resk){
						this.buy.id =this.detail.commoditySpecifications[k].id;
						this.buy.num = 1;
						this.buy.unitprice = this.detail.commoditySpecifications[k].price;
						this.buy.price =  this.buy.unitprice * this.buy.num;
						break;
						console.log(this.buy.price)
					}else{
						this.buy.id = 'null';
					}
				}

				
			},

			handleCancel(){
				
			},
			getDetail(){
				getCommodityDetailApi({
					commodityId: this.id
				}).then(res=>{
					console.log(res)
					this.detail = res.result
					this.buy.pic = this.detail.pic;
					this.buy.price = this.detail.price * 1;
					this.buy.commodityName = this.detail.commodityName;
				})
			},
			setSpecificationNames(){
				this.buy.specificationNames = "";
				for(let i = 0;i<this.buy.specifications.length;i++){
					this.buy.specificationNames = this.buy.specificationNames+",";
					this.buy.specificationNames = this.buy.specificationNames + this.buy.specifications[i].name;
					this.buy.specificationNames = this.buy.specificationNames +":"+ this.buy.specifications[i].itemName;
				}
				this.buy.specificationNames = this.buy.specificationNames.substring(1,this.buy.specificationNames.length);
			},
			goCart(){
				
				if(this.buy.id=="null"){
					this.setMessage('无此规格');
					return;
				}
				
				if(this.buy.specifications?.length==this.detail.specificationList?.length && this.buy.specifications?.length>0
				&& this.buy.id){
					
				}else{
					this.setMessage('请选择规格');
					return;
				}
				
				this.setSpecificationNames();
				
				 let shopcart = uni.getStorageSync("shopcart");
				 if(shopcart){
					 console.log(shopcart)
					 let shopcartjson = JSON.parse(shopcart);
					 
					 let shopcartjsonfi = shopcartjson.filter(item=>{
						 if(item.id==this.buy.id){
							 return false;
						 }else{
							 return true;
						 }
					 })
					 
					 shopcartjsonfi.push(this.buy);
					 uni.setStorageSync("shopcart",JSON.stringify(shopcartjsonfi));
				 }else{
					 let sc = [];
					 sc.push(this.buy);
					 uni.setStorageSync("shopcart",JSON.stringify(sc))
				 }
				 this.setMessage("加入购物车成功");
				
				//this.goPage('/pages/componentsC/uniformshop/uniformshopcart',{item:JSON.stringify(this.buy)});
				
			},
			goPay(){
				console.log(this.buy.specifications)
				console.log(this.detail.specifications)
				
				if(this.buy.id=="null"){
					this.setMessage('无此规格');
					return;
				}
				
				if(this.buy.specifications?.length==this.detail.specificationList?.length && this.buy.specifications?.length>0
				&& this.buy.id){
					
				}else{
					this.setMessage('请选择规格');
					return;
				}
				
				console.log(this.buy);
				this.setSpecificationNames();
				let reqparam = [];
				reqparam.push(this.buy);
				this.goPage('/pages/componentsC/uniformshop/uniformshopPay',{item:JSON.stringify(reqparam),isCart:false});
			}
		},
		onLoad({id,isOrder}) {
			if(isOrder==1) {
				this.isOrder = isOrder
				this.setPageTitle('订单详情')
			}
			this.id = this.$Route.query.id
			console.log('id='+this.id)
			console.log('id='+id)
			console.log(this.isOrder)
			if(this.id && this.isOrder==2) 
			this.getDetail()
			// console.log(this.$Route.query.item);
		},
		onShow() {
			
		},
	}
</script>

<style lang="scss" scoped>
.page{
	background-color: #f1f1f1;
	.container {
	    height: 100%;
	    background-color: #fff;
		padding-bottom: 50rpx;
	}
	.imageRow{
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 20rpx 20rpx;
		
	}
	.row {
	    width: 100%;
	    padding: 20rpx 20rpx;
	    display: flex;
	    align-items: center;
	    /* border-top: 2rpx solid #e6e6e6; */
	}
	
	.text {
	    // width: 100%;
	    height: fit-content;
	    color: #333;
	}
	
	.label {
	    color: #9e9e9e;
	    width: 100rpx;
	    margin-left: 30rpx;
	}
	
	.rowTitle {
	    padding: 20rpx;
	    margin: 0 30rpx;
	    position: relative;
	    border-top: 2rpx solid #e6e6e6;
	}
	
	.rowTitle::after {
	    content: '';
	    position: absolute;
	    top: 50%;
	    left: 0rpx;
	    transform: translateY(-50%);
	    width: 7rpx;
	    height: 36rpx;
	    background: #4089f0;
	}
	
	.footContent{
		    width: 750rpx;
			height: 186rpx;
			background: #FFFFFF;
			box-shadow: 0px 0px 20rpx 0px rgba(102, 102, 102, 0.05);
			border-radius: 50rpx 50rpx 0px 0px;
			position: fixed;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: space-around;
			font-size: 36rpx;
			padding: 0 20rpx;
			z-index: 10;
		.handleBtn{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 260rpx;
			height: 100rpx;
			background: #4089f0;
			border-radius: 50rpx;
			color: #fff;
		}
		.cancelBtn{
			background-color: #f9ae3d !important;
		}
	}
	.money{
		color: #cc463d; 
		font-size: 38rpx;
	}
		
}
</style>
